package com.xcity.pomelo.response;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.xcity.db.entity.Player;
import com.xcity.game.common.PlayerPool;
import com.xcity.game.recharge.RechargeTemplate;

import naga.x.game.time.TimeUpdater;
import naga.x.net.impl.pomelo.PomeloMessage;
import naga.x.net.impl.pomelo.protocol.Message;
import naga.x.util.TimeUtils;

@PomeloMessage(type = Message.Type.RESPONSE)
public class RechargeListResponse extends Response {

	public List<RechargeInfo> list;
	public int weekday;//周卡剩多少天
	public int moonday;//月卡剩多少天
	public int yearday;//0没充过  1充过
	
	public static class RechargeInfo {
		public int rechargeId;
		public String name;
		public String desc;
		public String icon;
		public int price;
		public int stone;
		public int count; // 已累充次数（用于判断首充是否双倍，首充不给双倍的永远为1）
	}
	
	public static interface Filter {
		boolean doFilter(int id);
	}
	
	public RechargeListResponse(int serial, Player player, List<RechargeTemplate> templates, Filter filter) {
		super(serial);
		if (templates == null || templates.isEmpty()) {
			list = Collections.emptyList();
			return;
		}
		list = new ArrayList<RechargeInfo>(templates.size());
		for (int i = 0; i < templates.size(); i++) {
			RechargeTemplate template = templates.get(i);
			if (template.isVisible() && (filter == null || filter.doFilter(template.getId()))) {
				RechargeInfo info = new RechargeInfo();
				info.rechargeId = template.getId();
				info.name = template.getName();
				info.desc = template.getDesc() != null ? template.getDesc() : "";
				info.icon = template.getIcon();
				info.price = template.getPrice();
				info.stone = template.getStone();
				info.count = template.isDoubleFirst() ? player.getPool().getIntValue(PlayerPool.PROPERTY_RECHARGE_COUNT_PREFIX + template.getId()) : 1;
				list.add(info);
			}
		}
		int today = TimeUpdater.getInstance().today();
		int lastWeekRecharge = player.getPool().getInt(PlayerPool.PROPERTY_LAST_RECHARGE_WEEK, 0);
		int weekTotalDays = player.getPool().getInt(PlayerPool.PROPERTY_WEEK_CARD_TOTALDAY, 0);
		weekday = TimeUtils.getDayGap(today, lastWeekRecharge + weekTotalDays);
		if(weekday < 0){
			weekday = 0;
		}

		int lastMoonRecharge = player.getPool().getInt(PlayerPool.PROPERTY_LAST_RECHARGE_MOON, 0);
		int moonTotalDays = player.getPool().getInt(PlayerPool.PROPERTY_MOON_CARD_TOTALDAY, 0);
		moonday = TimeUtils.getDayGap(today, lastMoonRecharge + moonTotalDays);
		if(moonday < 0){
			moonday = 0;
		}

		int lastYearRecharge = player.getPool().getInt(PlayerPool.PROPERTY_LAST_RECHARGE_YEAR, 0);
		if(lastYearRecharge > 0){
			yearday = 1;
		}
	}
	
}
